package com.mobi.cacti.ns.view;

import java.util.ArrayList;

import org.holoeverywhere.LayoutInflater;
import org.holoeverywhere.app.Activity;
import org.holoeverywhere.app.Fragment;
import org.holoeverywhere.widget.ListView;
import org.holoeverywhere.widget.ProgressBar;
import org.holoeverywhere.widget.TextView;

import com.mobi.cacti.R;
import com.mobi.cacti.ns.model.ListItem;
import com.mobi.cacti.ns.view.adapter.ItemListAdapter;
import com.mobi.cacti.ns.view.tab.BaseContainerFragment;
import com.mobi.cacti.rabbit.controllers.TicketListActivityController;
import com.mobi.cacti.rabbit.controllers.TicketListActivityControllerCallback;
import com.mobi.cacti.rabbit.response.Cache;
import com.mobi.cacti.rabbit.response.ReqTicketHistory;
import com.mobi.cacti.rabbit.response.ReqTicketRecent;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import at.bookworm.widget.SegmentedControlButton;

public class TicketListActivity extends Fragment implements
		TicketListActivityControllerCallback {

	private TicketListActivityController ticketListActivityController;
	private String circuitId;

	private ArrayList<ReqTicketRecent> recentArrayList;
	private ArrayList<ReqTicketHistory> historyArrayList;
	private ArrayList<ListItem> items;

	private SegmentedControlButton recentRadioButton;
	private SegmentedControlButton historyRadioButton;
	private TextView tableHeaderTextView;

	private ItemListAdapter adapter;

	private ProgressBar progressBar;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		ticketListActivityController = TicketListActivityController
				.getInstance();
		ticketListActivityController.delegate = this;

		Bundle bundle = getArguments();

		this.circuitId = bundle.getString("circuitId");

		items = new ArrayList<ListItem>();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = LayoutInflater.from(getActivity()).inflate(
				R.layout.ticketlist_layout, null);

		recentRadioButton = (SegmentedControlButton) v
				.findViewById(R.id.radio_recent);
		historyRadioButton = (SegmentedControlButton) v
				.findViewById(R.id.radio_history);
		tableHeaderTextView = (TextView) v.findViewById(R.id.tableheader);

		progressBar = (ProgressBar) v.findViewById(R.id.progressbar);

		// restore
		if (Cache.ticketListStateBundle != null) {
			int currentRadioButtonId = Cache.ticketListStateBundle.getInt(
					"selectedButton", 0);

			if (currentRadioButtonId == 0) {
				tableHeaderTextView.setText("Recents");
				currentRadioButtonId = recentRadioButton.getId();
				recentRadioButton.setChecked(true);

				// put default recent button id to cache
				Cache.ticketListStateBundle.putInt("selectedButton",
						currentRadioButtonId);

			} else if (currentRadioButtonId == recentRadioButton.getId()) {
				tableHeaderTextView.setText("Recents");
				currentRadioButtonId = recentRadioButton.getId();
				recentRadioButton.setChecked(true);

			} else if (currentRadioButtonId == historyRadioButton.getId()) {
				tableHeaderTextView.setText("History");
				currentRadioButtonId = historyRadioButton.getId();
				historyRadioButton.setChecked(true);
			}
		}

		if (recentRadioButton.isChecked()) {
			ticketListActivityController.loadRecentTicketListForCurcuitId(
					this.getActivity(), circuitId);
		} else if (historyRadioButton.isChecked()) {
			ticketListActivityController.loadHistoryTicketListForCurcuitId(
					this.getActivity(), circuitId);
		}

		recentRadioButton.setOnClickListener(radioButtonOnClickListener);
		historyRadioButton.setOnClickListener(radioButtonOnClickListener);

		adapter = new ItemListAdapter(getSupportActivity(),
				R.layout.rowitem_layout, items);
		ListView listView = (ListView) v.findViewById(R.id.ticketlist);
		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String ticketId = null;

				if (recentRadioButton.isChecked()) {
					ticketId = recentArrayList.get(position).ticketId;
				} else if (historyRadioButton.isChecked()) {
					ticketId = historyArrayList.get(position).ticketId;
				}

				if (ticketId != null) {
					Bundle bundle = new Bundle();
					bundle.putString("ticketId", ticketId);

					TicketDetailActivity ticketDetailActivity = new TicketDetailActivity();
					ticketDetailActivity.setArguments(bundle);

					((BaseContainerFragment) getParentFragment())
							.replaceFragment(ticketDetailActivity, true);
				}
			}
		});

		return v;
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);

		if (recentRadioButton == null || historyRadioButton == null) {
			return;
		}

		if (Cache.ticketListStateBundle == null) {
			Cache.ticketListStateBundle = new Bundle();
		}

		int currentRadioButtonId = 0;
		
		if (recentRadioButton.isChecked()) {
			currentRadioButtonId = recentRadioButton.getId();
		} else if (historyRadioButton.isChecked()) {
			currentRadioButtonId = historyRadioButton.getId();
		}
		
		// put selected items into cache
		Cache.ticketListStateBundle.putInt("selectedButton",
				currentRadioButtonId);
	}

	@Override
	public void ticketListActivityControllerDidLoadRecentTicketList(
			ArrayList<ReqTicketRecent> recentArrayList) {
		this.recentArrayList = recentArrayList;
		reloadListView();
	}

	@Override
	public void ticketListActivityControllerDidLoadHistoryList(
			ArrayList<ReqTicketHistory> historyArrayList) {
		this.historyArrayList = historyArrayList;
		reloadListView();
	}

	private void reloadListView() {
		progressBar.setVisibility(View.GONE);

		items.clear();

		if (recentRadioButton.isChecked() && recentArrayList != null) {
			for (ReqTicketRecent reqTicketRecent : recentArrayList) {
				ListItem item = new ListItem();
				item.label = reqTicketRecent.ticketId;
				item.ticketCountLabel = null;
				item.isShowRightArrow = false;
				items.add(item);
			}
		} else if (historyRadioButton.isChecked() && historyArrayList != null) {
			for (ReqTicketHistory reqTicketHistory : historyArrayList) {
				ListItem item = new ListItem();
				item.label = reqTicketHistory.ticketId;
				item.ticketCountLabel = null;
				item.isShowRightArrow = false;
				items.add(item);
			}
		}

		adapter.notifyDataSetChanged();
	}

	private Activity getThisFragmentActivity() {
		return (Activity) this.getActivity();
	}

	private OnClickListener radioButtonOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			progressBar.setVisibility(View.VISIBLE);

			int currentRadioButtonId = v.getId();

			if (Cache.ticketListStateBundle == null) {
				Cache.ticketListStateBundle = new Bundle();
			}

			// put selected items into cache
			Cache.ticketListStateBundle.putInt("selectedButton",
					currentRadioButtonId);

			if (v.getId() == recentRadioButton.getId()) {
				tableHeaderTextView.setText("Recents");

				ticketListActivityController.loadRecentTicketListForCurcuitId(
						getThisFragmentActivity(), circuitId);

				items.clear();
				adapter.notifyDataSetChanged();
			} else if (v.getId() == historyRadioButton.getId()) {
				tableHeaderTextView.setText("History");

				ticketListActivityController.loadHistoryTicketListForCurcuitId(
						getThisFragmentActivity(), circuitId);

				items.clear();
				adapter.notifyDataSetChanged();
			}
		}
	};

}
